﻿#include "SysTrayIcon.h"
#include "components/FWidgets/FMenu/FMenu.h"

#include <QAction>
#include <QApplication>

SysTrayIcon::SysTrayIcon(const QString &title, QWidget *parent)
    : QSystemTrayIcon{parent}
{
    FMenu* menu = new FMenu(title, parent);
    QAction* actShow = new QAction(tr("Show"), this);
    QAction* actExit = new QAction(tr("Exit"), this);
    menu->addAction(actShow);
    menu->addAction(actExit);
    setContextMenu(menu);
    setIcon(QIcon(":/imgs/favicon.ico"));
    setToolTip(title);

    connect(actShow, &QAction::triggered, this, &SysTrayIcon::showMainWindow);
    connect(actExit, &QAction::triggered, this, &SysTrayIcon::quit);
    connect(this, &SysTrayIcon::activated, this, [=](QSystemTrayIcon::ActivationReason reason){
        if (reason == QSystemTrayIcon::ActivationReason::Trigger) {
            showMainWindow();
        }
    });
}

void SysTrayIcon::show()
{
    QSystemTrayIcon::show();
    showMessage("Tip", tr("app has been hidden in system tray."), QSystemTrayIcon::MessageIcon::NoIcon);
}

void SysTrayIcon::showMainWindow()
{
    QWidget* p = qobject_cast<QWidget*>(parent());
    p->show();
    hide();
}

void SysTrayIcon::quit()
{
    qApp->quit();
}
